package cn.jiguang.jgadpubadapter.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import cn.jiguang.jgadpubadapter.Constants;
import cn.jiguang.jgadpubadapter.JgPubAdInit;
import cn.jiguang.jgadpubadapter.LogUtil;
import cn.jiguang.jgssp.ad.ADJgInterstitialAd;
import cn.jiguang.jgssp.ad.data.ADJgInterstitialAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgInterstitialAdListener;
import xyz.adscope.amps.ad.interstitial.adapter.AMPSInterstitialAdAdapterListener;
import xyz.adscope.amps.ad.interstitial.adapter.AMPSInterstitialAdapter;
import xyz.adscope.amps.base.AMPSBidResult;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.init.inter.IAMPSChannelInitCallBack;
import xyz.adscope.amps.inner.AMPSAdBiddingListener;
import xyz.adscope.amps.model.AMPSAdapterModel;

public class JgPuAdInterstitialAdapter extends AMPSInterstitialAdapter {

    private ADJgInterstitialAd adJgInterstitialAd;
    private ADJgInterstitialAdInfo adJgInterstitialAdInfo;

    //实现竞价功能时，重写该方法,如果渠道sdk不支持竞价可以不用重写该方法
    @Override
    public void startBid(Context context, AMPSAdapterModel ampsAdapterModel,
                         AMPSAdBiddingListener listener) {
        super.startBid(context, ampsAdapterModel, listener);
        initSDK();
    }

    //实现竞价功能时，重写该方法,如果渠道sdk不支持竞价可以不用重写该方法，发送给渠道竞价成功通知
    @Override
    public void sendWinNotice(AMPSBidResult ampsBidResult) {
        super.sendWinNotice(ampsBidResult);
        //需要调用渠道sdk方法通知渠道竞胜成功，ampsBidResult内容详情参考文档
    }

    //实现竞价功能时，重写该方法,如果渠道sdk不支持竞价可以不用重写该方法，发送给渠道竞败通知
    @Override
    public void sendLossNotice(AMPSBidResult ampsBidResult) {
        super.sendLossNotice(ampsBidResult);
        //需要调用渠道sdk方法通知渠道竞败，ampsBidResult内容详情参考文档
    }

    @Override
    public boolean isValid() {
        if (adJgInterstitialAd == null) {
            return false;
        }
        return true;
    }

    @Override
    public void showAd(Activity activity) {
        LogUtil.d(TAG, "showad: " );
        if (activity == null || adJgInterstitialAd ==null ||adJgInterstitialAdInfo == null) {
            LogUtil.d(TAG, "showAd: null" );
            return;
        }
        adJgInterstitialAdInfo.showInterstitial(activity);
    }

    private static final String TAG = "JgPuAdInterstitialAdapt";

    @Override
    public void destroy() {
        super.destroy();
        adJgInterstitialAd =null;
        adJgInterstitialAdInfo = null;
    }

    @Override
    public void loadNetworkAd(Context context, AMPSAdapterModel ampsAdapterModel, AMPSInterstitialAdAdapterListener listener) {
        super.loadNetworkAd(context, ampsAdapterModel, listener);
        if (isBidding && adJgInterstitialAd!=null) {
            JgPuAdInterstitialAdapter.this.onAdLoad();
            return;
        }
        initSDK();
    }



    private void initSDK() {
        JgPubAdInit.getInstance().initSDK(mInitAdapterConfig, new IAMPSChannelInitCallBack() {
            @Override
            public void successCallBack() {
                LogUtil.d(TAG, "successCallBack: jgpub adapter");
                loadAd();
            }

            @Override
            public void failCallBack(AMPSError ampsError) {
                LogUtil.d(TAG, "adapter init LoadFail " +
                        "code:" + ampsError.getCode() + " message:" + ampsError.getMessage());
                JgPuAdInterstitialAdapter.this.onAdFailed(ampsError.getCode(), ampsError.getMessage());
            }
        });
    }

    private int mEcpm = 0;
    private void loadAd() {
        adJgInterstitialAd = new ADJgInterstitialAd((Activity) mContext);
        adJgInterstitialAd.setListener(new ADJgInterstitialAdListener() {
            @Override
            public void onAdReady(ADJgInterstitialAdInfo adJgInterstitialAdInfo) {

            }

            @Override
            public void onAdReceive(ADJgInterstitialAdInfo adJgInterstitialAdInfo) {
                LogUtil.d(TAG, "onAdReceive: " );
                JgPuAdInterstitialAdapter.this.adJgInterstitialAdInfo = adJgInterstitialAdInfo;
                if (isBidding) {
                    mEcpm = (int) adJgInterstitialAdInfo.getECPM();
                    JgPuAdInterstitialAdapter.this.onC2SBiddingSuccess(mEcpm);
                } else {
                    JgPuAdInterstitialAdapter.this.onAdLoad();
                }
            }

            @Override
            public void onAdExpose(ADJgInterstitialAdInfo adJgInterstitialAdInfo) {
                JgPuAdInterstitialAdapter.this.onAdShow();
            }

            @Override
            public void onAdClick(ADJgInterstitialAdInfo adJgInterstitialAdInfo) {
                JgPuAdInterstitialAdapter.this.onAdClicked();
            }

            @Override
            public void onAdClose(ADJgInterstitialAdInfo adJgInterstitialAdInfo) {
                JgPuAdInterstitialAdapter.this.onAdDismiss();
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                JgPuAdInterstitialAdapter.this.onAdFailed(String.valueOf(adJgError.getCode()),adJgError.getError());
                LogUtil.d(TAG, "onAdFailed: "+ adJgError);
            }
        });
        adJgInterstitialAd.loadAd(mSpaceId);
    }
}
